package com.shop2cn.iapi.sqdistributor.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.BindMerchantDistributorRelationReq;
import com.shop2cn.iapi.sqdistributor.service.BindMerchantDistributorRelationCall;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_BindMerchantDistributorRelation {
	
	//java接口
	private static BindMerchantDistributorRelationReq bindMerchantDistributorRelationReq;
	private static BindMerchantDistributorRelationCall bindMerchantDistributorRelationCall;
	
	private static DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定商户分销员关系");
	}

	@Before
	public void caseUp() {
		bindMerchantDistributorRelationReq = new BindMerchantDistributorRelationReq();
		bindMerchantDistributorRelationCall = new BindMerchantDistributorRelationCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindMerchantDistributorRelationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(DistributorRelation distributorRelation) throws Exception {

		DistributorRelation actualResult = distributorRelationWrapper
				.selectByMerchantIdAndDistributorId(
						bindMerchantDistributorRelationReq.getMerchantId(),
						bindMerchantDistributorRelationReq.getDistributorId());

		Logger.verifyEquals(bindMerchantDistributorRelationReq.getMerchantId(),
				actualResult.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(
				bindMerchantDistributorRelationReq.getDistributorId(),
				actualResult.getDistributor_id(), "验证Distributor_id");
		Logger.verifyEquals(
				bindMerchantDistributorRelationReq.getDistributorName(),
				actualResult.getDistributor_name(), "验证Distributor_name");
		Logger.verifyEquals(
				distributorRelationWrapper.selectByDistributorId(
						bindMerchantDistributorRelationReq
								.getParentDistributorId())
						.getDistributor_name(), actualResult
						.getParent_distributor_name(), "验证Distributor_name");
		Logger.verifyEquals(
				bindMerchantDistributorRelationReq.getDistributorPhone(),
				actualResult.getDistributor_phone(), "验证Distributor_phone");
		Logger.verifyEquals(0, actualResult.getDistributor_level_index(),
				"验证Distributor_level_index");
		Logger.verifyEquals(
				bindMerchantDistributorRelationReq.getParentDistributorId(),
				actualResult.getParent_distributor_id(),
				"验证Parent_distributor_id");
		
		if (distributorRelation.getDistributor_type() == 1)
			Logger.verifyEquals(1001, actualResult
					.getDistributor_type(), "验证Distributor_types");
		else if (distributorRelation.getDistributor_type() == 2)
			Logger.verifyEquals(2001, actualResult
					.getDistributor_type(), "验证Distributor_type");
		else
			Logger.verifyEquals(Utils
					.zeroIfNull(bindMerchantDistributorRelationReq
							.getDistributorType()), actualResult
					.getDistributor_type(), "验证Distributor_type");
		
		Logger.verifyEquals(
				distributorRelation.getDirect_subordinate_count() + 1,
				distributorRelationWrapper.selectByDistributorId(
						bindMerchantDistributorRelationReq
								.getParentDistributorId())
						.getDirect_subordinate_count(),
				"验证Direct_subordinate_count");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_001() {
		Logger.start(true, "绑定商户分销员关系，绑定成功");
		
		try {
			
			DistributorRelation parentDistributor = distributorRelationWrapper
					.selectByDistributorId(ShenqiUserSet.indateRelationMerchantId);
			
			bindMerchantDistributorRelationReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindMerchantDistributorRelationReq.setParentDistributorId(ShenqiUserSet.indateRelationMerchantId);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(true,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(parentDistributor);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_002() {
		Logger.start(true, "绑定商户分销员关系，绑定成功");
		
		try {
			
			DistributorRelation parentDistributor = distributorRelationWrapper
					.selectByDistributorId(58082709L);
			
			bindMerchantDistributorRelationReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(500865146L);
			bindMerchantDistributorRelationReq.setParentDistributorId(58082709L);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(true,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(parentDistributor);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_003() {
		Logger.start(true, "商户没有parentDistributorId，绑定失败");
		
		try {
			
			bindMerchantDistributorRelationReq.setDistributorId(22222L);
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(111111L);
			bindMerchantDistributorRelationReq.setParentDistributorId(14120153L);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(false,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(String
					.format("商户%s下的分销员%s不存在！",
							bindMerchantDistributorRelationReq.getMerchantId(),
							bindMerchantDistributorRelationReq
									.getParentDistributorId()),
					bindMerchantDistributorRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_004() {
		Logger.start(true, "DistributorId为null，绑定失败");
		
		try {
			
			//bindMerchantDistributorRelationReq.setDistributorId(22222L);
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(111111L);
			bindMerchantDistributorRelationReq.setParentDistributorId(14120153L);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(false,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(
					"distributorId:must be greater than or equal to 1",
					bindMerchantDistributorRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_005() {
		Logger.start(true, "DistributorName为null，绑定失败");
		
		try {
			
			bindMerchantDistributorRelationReq.setDistributorId(22222L);
			// bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq
					.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(111111L);
			bindMerchantDistributorRelationReq
					.setParentDistributorId(14120153L);

			bindMerchantDistributorRelationCall
					.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();

			Logger.verifyEquals(false,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("distributorName:must not be empty",
					bindMerchantDistributorRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_006() {
		Logger.start(true, "DistributorPhone为null，绑定失败");
		
		try {
			
			bindMerchantDistributorRelationReq.setDistributorId(22222L);
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			//bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(111111L);
			bindMerchantDistributorRelationReq
					.setParentDistributorId(14120153L);

			bindMerchantDistributorRelationCall
					.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();

			Logger.verifyEquals(false,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("distributorPhone:must not be empty",
					bindMerchantDistributorRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_007() {
		Logger.start(true, "MerchantId为null，绑定失败");
		
		try {
			
			bindMerchantDistributorRelationReq.setDistributorId(22222L);
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			//bindMerchantDistributorRelationReq.setMerchantId(111111L);
			bindMerchantDistributorRelationReq
					.setParentDistributorId(14120153L);

			bindMerchantDistributorRelationCall
					.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();

			Logger.verifyEquals(false,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("merchantId:must be greater than or equal to 1",
					bindMerchantDistributorRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_008() {
		Logger.start(true, "ParentDistributorId为null，绑定失败");
		
		try {
			
			bindMerchantDistributorRelationReq.setDistributorId(22222L);
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(111111L);
			//bindMerchantDistributorRelationReq.setParentDistributorId(14120153L);

			bindMerchantDistributorRelationCall
					.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();

			Logger.verifyEquals(false,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("parentDistributorId:must be greater than or equal to 1",
					bindMerchantDistributorRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_009() {
		Logger.start(true, "绑定商户分销员机构关系，绑定成功");
		
		try {
			
			DistributorRelation parentDistributor = distributorRelationWrapper
					.selectByDistributorId(500865146L);
			
			bindMerchantDistributorRelationReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(500865146L);
			bindMerchantDistributorRelationReq.setParentDistributorId(500865146L);
			bindMerchantDistributorRelationReq.setDistributorType(1);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(true,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(parentDistributor);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_010() {
		Logger.start(true, "绑定商户CPS关系，绑定成功");
		
		try {
			
			DistributorRelation parentDistributor = distributorRelationWrapper
					.selectByDistributorId(500865146L);
			
			bindMerchantDistributorRelationReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(500865146L);
			bindMerchantDistributorRelationReq.setParentDistributorId(500865146L);
			bindMerchantDistributorRelationReq.setDistributorType(2);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(true,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(parentDistributor);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_011() {
		Logger.start(true, "分销员机构绑定分销员，绑定成功");
		
		try {
			
			DistributorRelation parentDistributor = distributorRelationWrapper
					.selectByDistributorId(1167266153L);
			
			bindMerchantDistributorRelationReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(500865146L);
			bindMerchantDistributorRelationReq.setParentDistributorId(1167266153L);
			bindMerchantDistributorRelationReq.setDistributorType(0);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(true,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(parentDistributor);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantDistributorRelation_012() {
		Logger.start(true, "CPS机构绑定分销员，绑定成功");
		
		try {
			
			DistributorRelation parentDistributor = distributorRelationWrapper
					.selectByDistributorId(730171354L);
			
			bindMerchantDistributorRelationReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			bindMerchantDistributorRelationReq.setDistributorName(TestDataManager.getChineseName());
			bindMerchantDistributorRelationReq.setDistributorPhone(TestDataManager.getPhoneNum());
			bindMerchantDistributorRelationReq.setMerchantId(500865146L);
			bindMerchantDistributorRelationReq.setParentDistributorId(730171354L);
			bindMerchantDistributorRelationReq.setDistributorType(0);
			
			bindMerchantDistributorRelationCall.setData(bindMerchantDistributorRelationReq);
			bindMerchantDistributorRelationCall.callService();
			
			Logger.verifyEquals(true,
					bindMerchantDistributorRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(parentDistributor);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
